<template>
  <div>
    <div class="main_div">
      <div class="title">
        <span>用户信息</span>
        <span class="edit_button" @click="dialogUpdateUser = true">
          <i class="el-icon-edit"></i>
          编辑
        </span>
      </div>
      <el-divider></el-divider>
        <div class="info-wapper">
          <div class="info-box">
            <div class="left_div">昵称</div>
            <div class="right_div">{{user.nickname}}</div>
          </div>
          <div class="info-box">
            <div class="left_div">职位</div>
            <div class="right_div">
              <span v-if="user.office==null">未设置</span>
              <span v-else>{{user.office}}</span>
            </div>
          </div>
          <div class="info-box">
            <div class="left_div">性别</div>
            <div class="right_div">
              <span v-if="user.gender==0">保密</span>
              <span v-else-if="user.gender==1">男</span>
              <span v-else-if="user.gender==2">女</span>
            </div>
          </div>
          <div class="info-box">
            <div class="left_div">个性签名</div>
            <div class="right_div" v-if="user.signature!=''">{{user.signature}}</div>
            <div class="right_div" v-else>无</div>
          </div>
        </div>
      </div>

    <!--编辑-->
    <el-dialog title="编辑个人信息"
               :visible.sync="dialogUpdateUser"
    >
      <el-form :model="userData"
               style="padding-top: 30px"
               label-width="80px"
               :rules="user_rules"
               v-loading="userLoading"
               ref="user_form">
        <el-form-item label="昵称"
                      prop="nickname">
          <el-input v-model="userData.nickname"
                    autocomplete="off"
                    class="inputDiv"
                    type="text"
          ></el-input>
        </el-form-item>
        <el-form-item label="职业">
          <el-select v-model="userData.office" placeholder="请选择职业" class="inputDiv">
            <el-option v-for="(item,index) in offices"
                       :label="item.name"
                       :value="item.name"
                       :key="index"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="性别" style="text-align: left">
          <el-radio-group v-model="userData.gender">
            <el-radio :label="0" :value="0">保密</el-radio>
            <el-radio :label="1" :value="1">男</el-radio>
            <el-radio :label="2" :value="2">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="个性签名">
          <el-input type="textarea"
                    v-model="userData.signature"
                    maxlength="200"
                    show-word-limit
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogUpdateUser = false">取 消</el-button>
        <el-button type="primary" @click="updateUser">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {mapState} from 'vuex'

export default {
  name: 'UserInfo',
  data () {
    return {
      dialogUpdateUser: false,
      userLoading: false,
      userData: {},
      user_rules: {
        nickname: [
          {required: true, message: '请输入用户昵称', trigger: 'blur'}
        ]
      },
      offices: [
        {
          name: '学生'
        },
        {
          name: 'Python工程师'
        },
        {
          name: 'Web前端工程师'
        },
        {
          name: 'JS工程师'
        },
        {
          name: 'PHP开发工程师'
        },
        {
          name: 'JAVA开发工程师'
        },
        {
          name: '移动开发工程师'
        },
        {
          name: 'Linux系统工程师'
        },
        {
          name: '产品经理'
        },
        {
          name: '数据库工程师'
        },
        {
          name: '软件工程师'
        },
        {
          name: 'UI设计师'
        },
        {
          name: '交互设计师'
        },
        {
          name: 'CG影视动画师'
        },
        {
          name: '全栈工程师'
        },
        {
          name: '算法工程师'
        },
        {
          name: '架构师'
        }
      ]
    }
  },
  computed: mapState({
    user: 'user'
  }),
  mounted () {
    this.getUser()
  },
  methods: {
    updateUser () {
      var _this = this
      this.$refs['user_form'].validate((valid) => {
        if (valid) {
          this.userLoading = true
          _this.$axios.post('/api/user/update',
            _this.$qs.stringify({
              userData: JSON.stringify(_this.userData)
            }))
            .then(function (response) {
              _this.userLoading = false
              var result = response.data
              if (result.isSuccess) {
                var user = JSON.parse(JSON.stringify(result.result))
                _this.dialogUpdateUser = false
                _this.$store.commit('setInfo', user)
                _this.$message({
                  message: '更新成功',
                  type: 'success'
                })
              } else {
                _this.$message({
                  message: result.msg,
                  type: 'warning'
                })
              }
            })
            // eslint-disable-next-line handle-callback-err
            .catch(function (error) {
              _this.userLoading = false
              _this.$message({
                message: '查询出错，请重新尝试',
                type: 'warning'
              })
            })
        } else {
          return false
        }
      })
    },
    getUser () {
      var _this = this
      _this.$axios.post('/api/user/getUserInfo',
        _this.$qs.stringify({
          userId: this.user.userId
        }))
        .then(function (response) {
          var data = response.data
          if (data.isSuccess) {
            _this.userData = data.result
          }
        })
    }
  }
}
</script>

<style>

  .inputDiv {
    width: 100%;
  }
  .el-dialog {
    width: 30%;
  }
  .main_div {
    margin-top: 20px;
  }
  .title {
    text-align: left;
    padding-left: 20px;
    font-size: 20px;
    font-weight: bolder;
  }
  .edit_button {
    float: right;
    font-size: 18px;
    font-weight: lighter;
    color: deepskyblue;
    cursor: pointer;
  }
  .main_div .el-divider--horizontal{
    margin: 12px 0px;
  }
  .info-wapper {
    margin: 24px auto 24px 40px;
    width: 800px;
  }
  .left_div {
    width: 120px;
    line-height: 20px;
    padding: 20px 0;
    text-align: center;
    background-color: #f3f5f7;
    color: #07111b;
    font-weight: 700;
    display: inline-block;
  }
  .right_div {
    width: 480px;
    line-height: 20px;
    padding: 20px 0 20px 22px;
    border-bottom: 1px solid #d9dde1;
    text-align: left;
    display: inline-block;
  }
.info-box{
  margin-bottom: 20px;
}
</style>
